<?php
class GoodsTypeModel extends Model{
	public $table= 'goods_type';
	//添加商品类型
	public function addGoodsType($Data){
		if(empty($Data)){
			$this->error='数据不能为空';
			return false;
		}
		$this->validate=array(
			array('gtname','nonull','类型名不能为空',2,1)
		);
		if($this->create($Data)){
			$db = M('goods_type');
			if($db->where(array('gtname'=>$Data['gtname']))->find()){
				$this->error='类型名已经存在';
				return false;
			}
			$state = $this->add();
			if($state){
				if($this->updateCache()){
					return $state;
				}
			}
			return false;
		}else{
			return false;
		}
	}
	//修改
	public function edit(){

		$state=$this->save();
		if($state){
			if($this->updatecache()){
				return $state;
			}
			return false;
		}else{
			return false;
		}
	}
	//删除
	public function dorp(){
		$gtid=Q('gtid',0,'intval');
		if($gtid){
			if($this->del("gtid=$gtid")){
				$this->updateCache();
				return true;
			}
			return false;
		}else{
			$this->error('删除失败');
		}
	}
	//更新缓存
	public function updateCache(){
		$GoodsTypeData = $this->all();
		$cacheData=array();
		foreach($GoodsTypeData as $GoodsType){
			$cacheData[$GoodsType['gtid']]=$GoodsType;
		}
		return cache("goodstype",$cacheData);
	}
}